/**
 * 
 */
package com.yuqinyi.blog.service;

import java.time.LocalDate;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.yuqinyi.blog.domain.User;

/**
 * @author yuqy
 *
 */
public interface UserRepository extends JpaRepository<User, Long> {

	@Override
	<S extends User> S save(S entity);

	@Query("select u from User u where u.lastName like ?1%")
	List<User> findByAndSort(String lastName, Sort sort);

	User findByIdCard(String idCard);

	@Query(value = "select * from User where ID_CARD = ?1", nativeQuery = true)
	User findByIdCard2(String idCard);

	@Query("select u from User u where u.idCard = ?1")
	User findByIdCard3(String idCard);

//	 @Query(value = "SELECT * FROM User WHERE LAST_NAME = ?1",
//	            countQuery = "SELECT count(*) FROM User WHERE LAST_NAME = ?1",
//	            nativeQuery = true)
//	    Page<User> findByLastNameWithPageable(String lastName, Pageable pageable);
	@Query(value = "select * form user where last_name=?1", countQuery = "select count(1) form user where last_name=?1", nativeQuery = true)
	Page<User> findByLastNameWithPageable(String lastName, Pageable pageable);

//	@Query("select u from User u where u.firstName = :firstName or u.lastName = :lastName")
//    List<User> findByFirstNameOrLastName(@Param("firstName") String firstName,
//                                         @Param("lastName") String lastName);
	@Query("select u from User u where u.firstName = :firstName or u.lastName = :lastName")
	List<User> findByFirstNameOrLastName(@Param("firstName") String firstName, @Param("lastName") String lastName);

//    List<User> findByDateOfBirth(@Param("date") LocalDate date);
	List<User> findByDateOfBirth(@Param("data") LocalDate date);

	@Override
	List<User> findAll(Sort sort);

//	@Modifying
//    @Query("update User u set u.firstName = ?1 where u.idCard = ?2")
//    int updateUser(String firstName, String idCard);
	@Modifying
	@Query("update User u set u.firstName = ?1 where u.idCard = ?2")
	int updateUser(String firstName, String idCard);

	void deleteByIdCard(String idCard);

	@Query("delete from User u where u.idCard = ?1")
	void deleteByIdCard2(String idCard);

	@Override
	@Modifying
	@Query(value = "delete form user where id = ?1", nativeQuery = true)
	void deleteById(Long id);

}
